App = {
	web3Provider: null,
	contracts: {},
	addr: "0x0c74ff77df9afe7a2d1fa08361af5c34267df881",
	init: function() {
		return App.initWeb3();
	},
	//初始化 web3
	initWeb3: function() {
		if(typeof web3 !== 'undefined') {
			console.log("metamask");
			App.web3Provider = web3.currentProvider;
		} else {
			console.log("local")
			window.history.go();
		}
		web3 = new Web3(App.web3Provider);
		return App.initContract();
	},
	initContract: function() {
		$.getJSON(BCConfig.url + 'res/RPS.json', function(data) {
			var AdoptionArtifact = data;
			App.contracts.Adoption = TruffleContract(AdoptionArtifact);
			App.contracts.Adoption.setProvider(App.web3Provider);
			BC.bcInstance = App.contracts.Adoption.at(App.addr);

			for(var i = 0; i < BC.onFinish.length; i++) {
				BC.onFinish[i]();
			}
			return "";
		});
	}
};

App.init();
BC = {
	onFinish: [],
	do: function(type, param) {
		var prom = new Promise(function(resolve, reject) {
			BC.preFunc();
			var p;
			if(type == "create") {
				p = BC.bcInstance.create({
					value: param.value
				});
			} else if(type == 'challenge') {
				p = BC.bcInstance.challenge(param.hashId, {value: param.value});
			} else if(type == 'generateBlindedBid') {
				p = BC.bcInstance.generateBlindedBid(param.choice, param.secret);
			} else if(type == 'bid') {
				p = BC.bcInstance.bid(param.hashId, param.blindedBid, {});
			} else if(type == 'reveal') {
				p = BC.bcInstance.reveal(param.hashId, param.choice, param.secret, {})
			} else if(type == 'checkAndSettle') {
				p = BC.bcInstance.settle(param.hashId, {});
			} else if(type == 'query') {
				p = BC.bcInstance.query.call(param.hashId);
			} else if(type == 'canAction') {
				p = BC.bcInstance.canAction.call(param.hashId, param.oper);
			}

			p.then(function(result) {
				if(type == "canAction" || type == "query"){
					resolve(result);
				}else{
					BC.success(result, resolve);
				}
			}).catch(function(err) {
				BC.error(err);
//				reject(err);
			});
		});
		prom.catch(console.log);
		return prom;

	},

	preFunc: function() {
		if(typeof BC.bcInstance == 'undefined') {
			return;
		} else {
			BC.index = layer.load(0, {
				shade: false
			});
		}

	},
	getBalance: function() {
		var contractBalance = web3.eth.getBalance(App.addr).toNumber();
		console.log("##### contractBalance: " + contractBalance);
		return contractBalance;
	},
	success: function(result, success) {
		layer.close(BC.index);
		if(result.receipt.status == 1) {
			layer.msg("成功：");
			console.log(result);
			success(result);
		} else {
			layer.msg("失败：");
			console.log(result);
		}
		BC.printLog(result);

	},
	error: function(err) {

		layer.msg("error：" + err.message);
		console.log(err);
	},
	printLog: function(obj) {
		var log = null;
		if(obj.logs.length > 0) {
			log = obj.logs[0];
		}
		return;
		if(log.event == "Debug") {
			console.log("\tDebug Event");
			console.log("\t----------------------------------------------");
			console.log("\tplayer1: " + log.args.player1);
			console.log("\tplayer2: " + log.args.player2);
			console.log("\tblindedBid1: " + log.args.blindedBid1);
			console.log("\tblindedBid2: " + log.args.blindedBid2);
			console.log("\tchoice1: " + log.args.choice1);
			console.log("\tchoice2: " + log.args.choice2);
			console.log("\tstake: " + log.args.stake);
			console.log("\twinner: " + log.args.winner);
			console.log("\tcreateTime: " + log.args.createTime);
			console.log("\tbiddingStartTime: " + log.args.biddingStartTime);
			console.log("\trevealStartTime: " + log.args.revealStartTime);
			console.log("\tstate: " + log.args.state);
		} else if(log.event == "Create") {
			console.log("\tCreate Event");
			console.log("\t----------------------------------------------");
			console.log("\tplayer: " + log.args.player);
			console.log("\thashId: " + log.args.hashId);
			console.log("\tstake: " + log.args.stake);
		} else if(log.event == "Destroy") {
			console.log("\tDestroy Event");
			console.log("\t----------------------------------------------");
			console.log("\tplayer: " + log.args.player);
			console.log("\thashId: " + log.args.hashId);
			console.log("\tstake: " + log.args.stake);
		} else if(log.event == "Challenge") {
			console.log("\tChallenge Event");
			console.log("\t----------------------------------------------");
			console.log("\tplayer: " + log.args.player);
			console.log("\thashId: " + log.args.hashId);
			console.log("\tstake: " + log.args.stake);
		} else if(log.event == "BlindedBid") {
			console.log("\tBlindedBid Event");
			console.log("\t----------------------------------------------");
			console.log("\tplayer: " + log.args.player);
			console.log("\tblindedBid: " + log.args.blindedBid);
		} else if(log.event == "Bid") {
			console.log("\tBid Event");
			console.log("\t----------------------------------------------");
			console.log("\tplayer: " + log.args.player);
			console.log("\thashId: " + log.args.hashId);
			console.log("\tblindedBid: " + log.args.blindedBid);
		} else if(log.event == "Reveal") {
			console.log("\tReveal Event");
			console.log("\t----------------------------------------------");
			console.log("\tplayer: " + log.args.player);
			console.log("\thashId: " + log.args.hashId);
			console.log("\tchoice: " + log.args.choice);
			console.log("\tsecret: " + log.args.secret);
			console.log("\tok: " + log.args.ok);
		} else if(log.event == "CheckAndSettle") {
			console.log("\tCheckAndSettle Event");
			console.log("\t----------------------------------------------");
			console.log("\tplayer: " + log.args.player);
			console.log("\thashId: " + log.args.hashId);
			console.log("\tresult: " + log.args.result);
		} else if(log.event == "Settle") {
			console.log("\tSettle Event");
			console.log("\t----------------------------------------------");
			console.log("\tplayer: " + log.args.player);
			console.log("\thashId: " + log.args.hashId);
			console.log("\tstake: " + log.args.stake);
		} else {
			console.log("\tUnknown Event: " + log.event);
		}
	}
}

//create: function(value, success) {
//		BC.preFunc();
//		BC.bcInstance.create({
//			value: value
//		}).then(function(result) {
//			BC.success(result,success);
//		}).catch(function(err) {
//			BC.error(err);
//		});
//	},
//	challenge: function(hashId, value, success) {
//		BC.preFunc();
//		BC.bcInstance.challenge(hashId, {
//			value: value
//		}).then(function(result) {
//			BC.success(result,success);
//		}).catch(function(err) {
//			BC.error(err);
//		});
//	},
//	generateBlindedBid:function(choice,secret,success) {
//		BC.preFunc();
//		BC.bcInstance.generateBlindedBid(choice, secret).then(function(result) {
//			BC.success(result,success);
//		})
//		.catch(function(err) {
//			BC.error(err);
//		});
//
//	},
//	
//	bid: function(hashId,blindedBid,success) {
//		BC.preFunc();
//		console.log("hashId："+hashId+" blindedBid："+blindedBid);
//		BC.bcInstance.bid(hashId, blindedBid, {}).then(function(result) {
//			BC.success(result,success);
//		}).catch(function(err) {
//			BC.error(err);
//		});
//	},
//
//	reveal: function(hashId,choice,secret, success) {
//		BC.preFunc();
//		BC.bcInstance.reveal(hashId, choice, secret, {}).then(function(result) {
//			BC.success(result,success);
//		}).catch(function(err) {
//			BC.error(err);
//		});
//	},
//	checkAndSettle: function(hashId,success) {
//		BC.bcInstance.settle(hashId, {}).then(function(result) {
//			success(result);
//		}).catch(function(err) {
//			BC.error(err);
//		});
//	},
//	query:function(hashId,success){
//		BC.preFunc();
//		BC.bcInstance.query.call(hashId).then(function(result) {
//			console.log("#########query.call");
//			console.log(result);
//			layer.closeAll();
//			success(result);
//			
//		}).catch(function(err){
//			BC.error(err);
//		})
//	},
//	canAction:function(hashId,oper,success){
//		BC.preFunc();
//		BC.bcInstance.canAction.call(hashId,oper).then(function(result) {
//			console.log("#########canAction.call");
//			console.log(result);
//			layer.closeAll();
//			success(result);
//		}).catch(function(err){
//			BC.error(err);
//		})
//	},